﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.BLL;
using Wsitech.Model;

public partial class Backend_JobAdd : BackendPageBase
{
    protected readonly string jobid = HttpContext.Current.Request["jobid"] ?? "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["back"] = Request.UrlReferrer.ToString();
            bindCompany();
            if (jobid != "")
            {
                initPosition(jobid);
            }
        }
    }
    //初始化职位信息
    private void initPosition(string jobid)
    {
        PositionInfo job = PositionInfoBLL.Instance.GetDetail(jobid);
        if (job != null)
        {
            ddlCompany.SelectedValue = job.CompanyId;

            txtPositionName.Text = job.PositionName;
            txtDepartment.Text = job.Department;
            txtWorkPlace.Text = job.WorkPlace;
            txtResponsibility.Text = job.Responsibility;
            txtRequirements.Text = job.Requirements;
            txtContracts.Text = job.Contracts;
        }
    }

    //绑定公司
    private void bindCompany()
    {
        ddlCompany.DataSource = CompanyInfoBLL.Instance.GetList();
        ddlCompany.DataValueField = "CompanyId";
        ddlCompany.DataTextField = "CompanyName";
        ddlCompany.DataBind();

        ddlCompany.SelectedValue = CompanyInfoBLL.COMPANY_LEGEND_ID;
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (CheckParams())
        {
            if (jobid == "")
            {

                PositionInfoBLL.Instance.AddPosition(
                    ddlCompany.SelectedValue,
                    txtPositionName.Text.Trim(),
                    txtDepartment.Text.Trim(),
                    txtWorkPlace.Text.Trim(),
                    txtResponsibility.Text.Trim(),
                    txtRequirements.Text.Trim(),
                    txtContracts.Text.Trim()
                    );

            }
            else
            {
                PositionInfo job = PositionInfoBLL.Instance.GetDetail(jobid);
                if (job != null)
                {
                    job.CompanyId = ddlCompany.SelectedValue;
                    job.PositionName = txtPositionName.Text.Trim();
                    job.Department = txtDepartment.Text.Trim();
                    job.WorkPlace = txtWorkPlace.Text.Trim();
                    job.Responsibility = txtResponsibility.Text.Trim();
                    job.Requirements = txtRequirements.Text.Trim();
                    job.Contracts = txtContracts.Text.Trim();

                    job.UpdateTime = DateTime.Now;

                    PositionInfoBLL.Instance.Update(job);
                }
            }
            Response.Redirect(ViewState["back"].ToString());
        }
    }

    private bool CheckParams()
    {

        if (txtPositionName.Text.Trim() == "")
        {
            base.Alert("职位名称不能为空！");
            return false;
        }
        else if (txtPositionName.Text.Trim().Length > 25)
        {
            base.Alert("职位名称不能超过25字！");
            return false;
        }
        if (txtDepartment.Text.Trim() == "")
        {
            base.Alert("部门名称不能为空！");
            return false;
        }
        else if (txtDepartment.Text.Trim().Length > 25)
        {
            base.Alert("部门名称不超过25字！");
            return false;
        }
        if (txtWorkPlace.Text.Trim() == "")
        {
            base.Alert("工作地点不能为空！");
            return false;
        }
        else if (txtWorkPlace.Text.Trim().Length > 10)
        {
            base.Alert("工作地点不超过10字！");
            return false;
        }
        if (txtResponsibility.Text.Trim() == "")
        {
            base.Alert("工作职责不能为空！");
            return false;
        }
        else if (txtResponsibility.Text.Trim().Length > 5000)
        {
            base.Alert("职责描述不超过5000字！");
            return false;
        }
        if (txtRequirements.Text.Trim() == "")
        {
            base.Alert("任职资格不能为空！");
            return false;
        }
        else if (txtRequirements.Text.Trim().Length > 5000)
        {
            base.Alert("任职资格不超过5000字！");
            return false;
        }
        if (txtContracts.Text.Trim() == "")
        {
            base.Alert("联系方式不能为空！");
            return false;
        }
        else if (txtContracts.Text.Trim().Length > 5000)
        {
            base.Alert("联系方式不超过5000字！");
            return false;
        }

        return true;
    }
}